package eiisan.reflect;

import java.util.concurrent.CountDownLatch;

public class SynchronizedTest1 {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        Thread t = new Thread() {
            public void run() {
                latch.countDown();
                System.out.print("pong");
            }
        };
        t.start();
        latch.await();//等一下线程t
        System.out.print("ping");
    }

    public static void main2(String[] args){
        Test test1 = new Test();
        Thread thread1 = new Thread(test1);
        Thread thread2 = new Thread(test1);
        thread1.start();
        thread2.start();
    }
}

class Test implements Runnable{
    public synchronized void hello() throws Exception{
        System.out.println(Thread.currentThread().getName() + " say hello!!");
        sorry();
        System.out.println(Thread.currentThread().getName() + " helloed");
    }
    public synchronized void sorry() throws Exception{
        System.out.println(Thread.currentThread().getName() + " say sorry!!");
        throw new Exception(Thread.currentThread().getName() + " this is a test!");
    }
    public void run() {
        try {
            hello();
        }catch (Exception e){
            System.out.println(Thread.currentThread().getName() + " exception once");
        }
        try {
            Thread.sleep(10000L);
            System.out.println(Thread.currentThread().getName() + " exit");
        }catch (Exception e){
            System.out.println(Thread.currentThread().getName() + " some exception");
        }
    }
}